// pages/math/fourth/fourth.js
var config = require('../../../config')
const recordService = require('../../../services/recordService.js')
const sharedService = require('../../../services/sharedService.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topicString: '',                              // 数学题数
    goldNum: 0,                                  // 金币个数
    resultNum: '',                                // 题目正确结果
    inputNum: '',                                 // 用户输入结果
    disabled: false,                              // 用于禁用按钮
    questionNum: config.basic.questionNum,              // 总题数(计数使用)
    totalQuestionNum: config.basic.questionNum,         // 总题数
    cn: 0,                                        // 正确题数
    en: 0,                                        // 错误题数
    st: true,                                     // 返回挑战成功或者失败的状态码
    animationGold: {}                              // 金币动画
  },
  questionBank: function () {
    var numArr = [];
    var issueString = '';
    var resultString = 0;
    this.setData({
      disabled: false
    });

    if (this.data.questionNum == 0) {
      var accuracy = Math.round(this.data.cn / this.data.totalQuestionNum * 100);
      if (accuracy < 80) {
        this.setData({
          st: false
        })
      }
      setTimeout(function () {
        wx.redirectTo({
          url: `../../result/result?qn=${this.data.totalQuestionNum}&cn=${this.data.cn}&en=${this.data.en}&ac=${accuracy}&st=${this.data.st}`
        });
      }.bind(this), 500)
    }

    if (parseInt(Math.random() * 2) == 0) {
      for (var i = 0; i < 2;) {
        var addNum = parseInt(Math.random() * 30);
        if (i == 1) {
          if (numArr[0] + addNum <= 30) {
            numArr.push(addNum);
            i++;
          }
        } else {
          numArr.push(addNum);
          i++;
        }
      }
      issueString = numArr[0] + ' + ' + numArr[1] + ' = ';
      resultString = numArr[0] + numArr[1];
    } else {
      for (var i = 0; i < 2; i++) {
        numArr.push(parseInt(Math.random() * 30));
      }
      if (numArr[0] >= numArr[1]) {
        issueString = numArr[0] + ' - ' + numArr[1] + ' = ';
        resultString = numArr[0] - numArr[1];
      } else {
        issueString = numArr[1] + ' - ' + numArr[0] + ' = ';
        resultString = numArr[1] - numArr[0];
      }
    }

    this.setData({
      topicString: issueString,
      resultNum: resultString
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.questionBank();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(options){
    return sharedService.onShareAppMessage(options, function() {
      recordService.get()
    });
  },

  numBtn: function (e) {
    if (this.data.inputNum.length != this.data.resultNum.toString().length) {
      this.setData({
        inputNum: this.data.inputNum + e.target.id + ''
      });
    };
    if (this.data.inputNum.length == this.data.resultNum.toString().length) {
      this.setData({
        disabled: true
      });
      if (this.data.inputNum == this.data.resultNum) {
        this.correctAnimation();
        setTimeout(function () {
          wx.showToast({
            title: '正确',
            image: '../../images/suc.png',
            duration: 500
          });
          this.setData({
            resultNum: '',
            inputNum: '',
            goldNum: this.data.goldNum + 1,
            questionNum: this.data.questionNum - 1,
            cn: this.data.cn + 1
          });
          this.questionBank();
        }.bind(this), 500);
      } else {
        this.defeatAnimation();
        setTimeout(function () {
          wx.showToast({
            title: '错误',
            image: '../../images/fail.png',
            duration: 500
          });
          this.setData({
            resultNum: '',
            inputNum: '',
            questionNum: this.data.questionNum - 1,
            en: this.data.en + 1
          });
          this.questionBank();
        }.bind(this), 500);
      }
    }
  },
  clearBtn: function () {
    this.setData({
      inputNum: ''
    });
  },
  // 答题正确动画
  correctAnimation: function () {
    var animation = wx.createAnimation({
      duration: 500
    })

    this.animation = animation;

    animation.translateY(-130).opacity(0).width(60 + 'rpx').height(60 + 'rpx').step();

    this.setData({
      animationGold: animation.export()
    })

    setTimeout(function () {
      var animation = wx.createAnimation({
        duration: 0
      })
      animation.translateY(0).opacity(1).width(108 + 'rpx').height(108 + 'rpx').step();
      this.setData({
        animationGold: animation.export()
      })
    }.bind(this), 500)
  },
  // 答题错误动画
  defeatAnimation: function () {
    var animation = wx.createAnimation({
      duration: 500
    })

    this.animation = animation;

    animation.translateY(50).opacity(0).step();

    this.setData({
      animationGold: animation.export()
    })

    setTimeout(function () {
      var animation = wx.createAnimation({
        duration: 0
      })
      animation.translateY(0).opacity(1).step();
      this.setData({
        animationGold: animation.export()
      })
    }.bind(this), 500)
  }
})
